package com.way.filemanager.fragment;

import android.app.ActionBar;
import android.app.Activity;

import hwdroid.dialog.AlertDialog;
import hwdroid.dialog.Dialog;
import hwdroid.dialog.DialogInterface;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaScannerConnection;
import android.media.ThumbnailUtils;
import android.os.Handler;
import android.provider.MediaStore.Images;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;

import com.way.filemanager.FileManagerAppFrame;
import com.way.filemanager.R;
import com.way.filemanager.fragment.FileList.IconDrawableManager;
import com.way.filemanager.fragment.FileList.IconDrawableManager.GetIconCb;
import com.way.filemanager.navigation.MyFileManager.FileOrder;
import com.way.filemanager.navigation.NavigationListFragmant.Mode;
import com.way.filemanager.navigation.NavigationListFragmant.SelectMode;
import com.way.filemanager.preferences.FileManagerSettings;
import com.way.filemanager.preferences.Preferences;
import com.way.filemanager.providers.FilelistManager;
import com.way.filemanager.providers.FilelistManager.ChangeStatusCb;
import com.way.filemanager.ui.policy.IntentsActionPolicy;
import com.way.filemanager.util.AndroidHelper;
import com.way.filemanager.util.CommonFunc;
import com.way.filemanager.util.CommonIdDef;
import com.way.filemanager.util.FileCrushDialog;
import com.way.filemanager.util.FileDeleteDialog;
import com.way.filemanager.util.FileDetailDialog;
import com.way.filemanager.util.FileInfo;
import com.way.filemanager.util.FileRenameDialog;
import com.way.filemanager.util.ScrollOverListView;
import com.way.filemanager.util.FileCrushDialog.FileCrushDialog_OnResultCb;
import com.way.filemanager.util.FileDeleteDialog.FileDeleteDialog_OnResultCb;
import com.way.filemanager.util.FileRenameDialog.FileRenameDialog_OnResultCb;
import com.way.filemanager.util.ScrollOverListView.OnScrollOverListener;

import java.io.File;
import java.io.InvalidClassException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import hwdroid.widget.ActionBar.ActionBarView;
import hwdroid.widget.ActionBar.ActionBarView.OnBackKeyItemClick;
import hwdroid.widget.ActionBar.ActionBarView.OnRightWidgetItemClick;
import hwdroid.widget.ActionBar.ActionBarView.OnRightWidgetItemClick2;
import hwdroid.widget.FooterBar.FooterBarButton;
import hwdroid.widget.FooterBar.FooterBarMenu;
import hwdroid.widget.FooterBar.FooterBarType.OnFooterItemClick;
import hwdroid.widget.itemview.DrawableText2ItemView;

public class CategorySubtypeView implements ChangeStatusCb, FileRenameDialog_OnResultCb,
                                GetIconCb ,FileDeleteDialog_OnResultCb,FileCrushDialog_OnResultCb {
    private static final String TAG = "CategorySubtypeView";
    Activity mActivity;
    ViewGroup mSubTypesView;
    int mTypes;
    int mCategorySubtypeMode;
    ScrollOverListView mmSubTypesList;
    CategorySubtypeListAdapter mListAdapter;
    TextView mNoItemImageView;
    FilelistManager mFileListManager;
    LinearLayout mTitle;
    FooterBarMenu mFooterBarMenu;
    FooterBarButton mFooterBarButton;
    int mMenuItemId;
    CategorySubtypeView mThis;
    IconDrawableManager mIconDrawableManager;
    Handler mHandler;
    public CategorySubtypeView(Activity main,FilelistManager m,Handler h)
    {
        mActivity = main;
        mFileListManager = m;
        mThis = this;
        mHandler = h;
        mIconDrawableManager = new IconDrawableManager(mActivity,null,this);
    }
    public int init(ViewGroup v,int types)
    {
        mSubTypesView = v;
        mTypes = types;
        mFileListManager.startNormal(mTypes,this);
        mmSubTypesList = (ScrollOverListView)mSubTypesView.findViewById(R.id.subtypes_listView_id);
        initIconMgr();
        mListAdapter = new CategorySubtypeListAdapter(mActivity,mFileListManager,types,mIconDrawableManager);
        initCrumbs();
        //mTitle.addView(titleSub);
        mCategorySubtypeMode = CommonIdDef.CATEGORY_SUBTYPE_MODE_NORMAL;
        mmSubTypesList.setAdapter(mListAdapter);
        mNoItemImageView = (TextView)mSubTypesView.findViewById(R.id.subtypes_no_item_id);
        /*
        if(mListAdapter.getCount() == 0)
        {
            mNoItemImageView.setVisibility(View.VISIBLE);
            mmSubTypesList.setVisibility(View.INVISIBLE);
        }
        else
        {
            mNoItemImageView.setVisibility(View.INVISIBLE);
            mmSubTypesList.setVisibility(View.VISIBLE);
        }*/
        RefreshItemCount();
        initFootButton();
        //mmSubTypesList.setSelection(SubtypePicListAdapter.SubTypeConstItemNum);
        //mmSubTypesList.scrollTo(0, mActivity.getResources().getDimensionPixelOffset(R.dimen.default_row_height)/*DensityUtil.dip2px(mActivity, 48)*/);
        //mmSubTypesList.scrollListBy(1);
        mmSubTypesList.setOnItemClickListener(mOnItemClickListener);
        mmSubTypesList.setOnItemLongClickListener(mOnItemLongClickListener);
        mmSubTypesList.setOnScrollOverListener(mOnScrollOverListener);
        mmSubTypesList.setTopPosition(0);
        mmSubTypesList.setOnScrollListener(new OnScrollListener(){

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                // TODO Auto-generated method stub
                //Log.v(TAG, "mmSubTypesList firstVisibleItem:"+firstVisibleItem);
                //Log.v(TAG, "mmSubTypesList visibleItemCount:"+visibleItemCount);
            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                Log.v(TAG, "mmSubTypesList scrollState:"+scrollState);
                if(scrollState == SCROLL_STATE_IDLE)
                {
                 mIconDrawableManager.SetFocusPosition(mmSubTypesList.getFirstVisiblePosition(),
                        mmSubTypesList.getLastVisiblePosition());
                 mListAdapter.notifyDataSetChanged();
                 RefreshItemCount();
                }
            }});
        mListAdapter.SetAdapterMode(CommonIdDef.CATEGORY_SUBTYPE_MODE_NORMAL);
        return 0;
    }

    OnScrollOverListener mOnScrollOverListener = new OnScrollOverListener()
    {

        @Override
        public boolean onListViewTopAndPullDown(MotionEvent event, int delta) {
            // TODO Auto-generated method stub

            if((delta > 20)&&(mListAdapter.GetAdapterMode() == CommonIdDef.CATEGORY_SUBTYPE_MODE_NORMAL))
            {
                mListAdapter.SetAdapterMode(CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCH_BTN);
                mListAdapter.notifyDataSetChanged();
                mmSubTypesList.setSelection(0);
            }
            return false;
        }

        @Override
        public boolean onListViewBottomAndPullUp(MotionEvent event, int delta) {
            // TODO Auto-generated method stub
            //Log.v(TAG, "onListViewBottomAndPullUp");
            return false;
        }

        @Override
        public boolean onMotionDown(MotionEvent ev) {
            // TODO Auto-generated method stub
            //Log.v(TAG, "onMotionDown");
            return false;
        }

        @Override
        public boolean onMotionMove(MotionEvent ev, int delta) {
            // TODO Auto-generated method stub
            //Log.v(TAG, "onMotionMove");
            return false;
        }

        @Override
        public boolean onMotionUp(MotionEvent ev) {
            // TODO Auto-generated method stub
            //Log.v(TAG, "onMotionUp");

            return false;
        }

    };
    OnItemClickListener mOnItemClickListener = new OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            Log.v(TAG, "onItemClick");
            if((mCategorySubtypeMode == CommonIdDef.CATEGORY_SUBTYPE_MODE_COPY)
                    ||(mCategorySubtypeMode == CommonIdDef.CATEGORY_SUBTYPE_MODE_DELETE)
                    ||(mCategorySubtypeMode == CommonIdDef.CATEGORY_SUBTYPE_MODE_CUT)
                    ||(mCategorySubtypeMode == CommonIdDef.CATEGORY_SUBTYPE_MODE_SHARE))
            {
                SelectItemClick(arg1,arg1.getId());
            }
            else
                ListItemClick(arg1 ,arg1.getId());
        }

    };
    OnItemLongClickListener mOnItemLongClickListener = new OnItemLongClickListener()
    {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            Log.v(TAG, "onItemLongClick");
            if(arg1.getId() != CommonIdDef.SEARCH_ITEM_POSITON)
                StartItemLongPressClick(arg1.getId());
            return true;
        }

    };
    void initFootButton()
    {
        mFooterBarMenu = new FooterBarMenu(mActivity);

        if(mFooterBarMenu != null)
        {
            Log.v(TAG, "mFooterBarMenu:" + mFooterBarMenu);
            mFooterBarMenu.setOnFooterItemClick(mOnFooterItemClick);
            LinearLayout mfootbar_menu_id = (LinearLayout)mSubTypesView.findViewById(R.id.footbar_menu_id);
            mfootbar_menu_id.addView(mFooterBarMenu);

            RefreshFootMenu();
        }
    }
    void ClearFootMenu()
    {
        if(mFooterBarMenu != null)
        {
            mFooterBarMenu.clear();
        }
        if(mFooterBarButton != null)
        {
            mFooterBarButton.clear();
        }
        LinearLayout mfootbar_menu_id = (LinearLayout)mSubTypesView.findViewById(R.id.footbar_menu_id);
        mfootbar_menu_id.setVisibility(View.INVISIBLE);
        mfootbar_menu_id.removeAllViews();
    }
    void RefreshFootMenu()
    {
        if(mFooterBarMenu != null)
        {
            ClearFootMenu();
            LinearLayout mfootbar_menu_id = (LinearLayout)mSubTypesView.findViewById(R.id.footbar_menu_id);
            if(mCategorySubtypeMode == CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCHING)
            {
                mfootbar_menu_id.setVisibility(View.GONE);
                return;
            }
            mfootbar_menu_id.setVisibility(View.VISIBLE);
            mfootbar_menu_id.addView(mFooterBarMenu);
            mFooterBarMenu.addItem(CommonIdDef.FOOTBAR_ITEM_ID_SORT,
                    mActivity.getResources().getText(R.string.actions_menu_Sort),
                    mActivity.getResources().getDrawable(R.drawable.sort_menu));
            mFooterBarMenu.addItem(CommonIdDef.FOOTBAR_ITEM_ID_COPY,
                    mActivity.getResources().getText(R.string.actions_menu_Copy),
                    mActivity.getResources().getDrawable(R.drawable.copy_menu));
            mFooterBarMenu.addItem(CommonIdDef.FOOTBAR_ITEM_ID_DELETE,
                    mActivity.getResources().getText(R.string.actions_menu_Delete),
                    mActivity.getResources().getDrawable(R.drawable.delete_menu));
            mFooterBarMenu.addItem(CommonIdDef.FOOTBAR_ITEM_ID_CUT,
                    mActivity.getResources().getText(R.string.actions_menu_Cut),
                    mActivity.getResources().getDrawable(R.drawable.more_menu));
            mFooterBarMenu.addItem(CommonIdDef.FOOTBAR_ITEM_ID_SHARE,
                    mActivity.getResources().getText(R.string.actions_menu_Share),
                    mActivity.getResources().getDrawable(R.drawable.more_menu));

           /* mFooterBarMenu.addItem(CommonIdDef.FOOTBAR_ITEM_ID_NEW_DIR,
                    mActivity.getResources().getText(R.string.actions_menu_NewDir),
                    mActivity.getResources().getDrawable(R.drawable.hw_footerbar_item_more_icon_normal));*/

            /*int hideshow_id;
            Preferences.getSharedPreferences();
            FileManagerSettings showHiddenPref = FileManagerSettings.SETTINGS_SHOW_HIDDEN;
            if(Preferences.getSharedPreferences().getBoolean(showHiddenPref.getId(),
                     ((Boolean) showHiddenPref.getDefaultValue()).booleanValue()))
                hideshow_id = R.string.actions_menu_NotShowHideFile;
            else
                hideshow_id = R.string.actions_menu_ShowHideFile;

                mFooterBarMenu.addItem(CommonIdDef.FOOTBAR_ITEM_ID_SHOW_HIDE_FILE,
                        mActivity.getResources().getText(hideshow_id),
                        mActivity.getResources().getDrawable(R.drawable.hw_footerbar_item_more_icon_normal));
            */
            if(mFileListManager.GetWorkingFileCount() < 1)
            {
            mFooterBarMenu.setItemEnable(CommonIdDef.FOOTBAR_ITEM_ID_SORT, false);
            mFooterBarMenu.setItemEnable(CommonIdDef.FOOTBAR_ITEM_ID_COPY, false);
            mFooterBarMenu.setItemEnable(CommonIdDef.FOOTBAR_ITEM_ID_DELETE, false);
            mFooterBarMenu.setItemEnable(CommonIdDef.FOOTBAR_ITEM_ID_CUT, false);
            mFooterBarMenu.setItemEnable(CommonIdDef.FOOTBAR_ITEM_ID_SHARE, false);
            }
            else
            {
                mFooterBarMenu.setItemEnable(CommonIdDef.FOOTBAR_ITEM_ID_SORT, true);
                mFooterBarMenu.setItemEnable(CommonIdDef.FOOTBAR_ITEM_ID_COPY, true);
                mFooterBarMenu.setItemEnable(CommonIdDef.FOOTBAR_ITEM_ID_DELETE, true);
                mFooterBarMenu.setItemEnable(CommonIdDef.FOOTBAR_ITEM_ID_CUT, true);
                mFooterBarMenu.setItemEnable(CommonIdDef.FOOTBAR_ITEM_ID_SHARE, true);
            }
            mFooterBarMenu.setPrimaryItemCount(3);
            mFooterBarMenu.updateItems();
        }

    }
    void AddToFootButton(int FuncId,int FuncString,OnFooterItemClick m)
    {
        ClearFootMenu();
        if(mFooterBarButton == null)
            mFooterBarButton = new FooterBarButton(mActivity);

        LinearLayout mfootbar_menu_id = (LinearLayout)mSubTypesView.findViewById(R.id.footbar_menu_id);
        mfootbar_menu_id.setVisibility(View.VISIBLE);
        mfootbar_menu_id.addView(mFooterBarButton);

        mFooterBarButton.setOnFooterItemClick(m);
        mFooterBarButton.addItem(CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_CANCEL,
                mActivity.getResources().getText(R.string.cancel));
        mFooterBarButton.addItem(FuncId,
                mActivity.getResources().getText(FuncString));
        mFooterBarButton.setItemTextColor(FuncId, Color.GRAY);
        mFooterBarButton.setItemEnable(FuncId, false);
        mFooterBarButton.updateItems();
        mFooterBarButton.setClickable(false);


    }
    public int uninit()
    {
        mIconDrawableManager.ForceStopScan();
        mmSubTypesList.setAdapter(null);
        mListAdapter = null;
        mFileListManager.ClearWorkingStatus();
        return 0;
    }
    void ListItemClick(View view, int id)
    {
        if(id == CommonIdDef.SEARCH_ITEM_POSITON)
        {
            StartTypeSearch();
        }
        else
        {
            FileInfo info = mFileListManager.getCurrentFileInfo(id);
            IntentsActionPolicy.openFileSystemObject(mActivity, info.GetFileSystemObject(),
                    false, null, null);
        }
    }
    SearchView mSearchView;
    public void StartTypeSearch()
    {
        mCategorySubtypeMode = CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCHING;
        ((FileManagerAppFrame)mActivity).SetCanViewPagerMove(false);
        ActionBar bar = mActivity.getActionBar();


        LayoutInflater mInflater = LayoutInflater.from(mActivity);
        View mSearchViewLayer = mInflater.inflate(R.layout.category_search_bar, null);
        mSearchView = (SearchView)mSearchViewLayer.findViewById(R.id.Serachbar_view);
        mSearchView.setIconified(false);
        ImageView mSearchBack = (ImageView)mSearchViewLayer.findViewById(R.id.Searchbar_back);
        //mSearchBack.setClickable(true);
        mSearchBack.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mSearchView.clearFocus();
                BackToUpLevel();
            }
        });
        mSearchView.setOnQueryTextListener(mOnQueryTextListener);

        RefreshFootMenu();
        mFileListManager.startSearch(mTypes,this);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(false);
        bar.setDisplayShowCustomEnabled(true);
        bar.setCustomView(mSearchViewLayer);
        mListAdapter.SetAdapterMode(CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCHING);
        mListAdapter.notifyDataSetChanged();
        CommonFunc.TA_Click(CommonFunc.OnClick_Search);
        //mmSubTypesList.setSelection(0);
    }
    public void StartTypeNormal()
    {
        mFileListManager.startNormal(mTypes,this);
        mCategorySubtypeMode = CommonIdDef.CATEGORY_SUBTYPE_MODE_NORMAL;
        ((FileManagerAppFrame)mActivity).SetCanViewPagerMove(true);
        ActionBar bar = mActivity.getActionBar();
        RefreshFootMenu();

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(false);
        bar.setDisplayShowCustomEnabled(false);

        mListAdapter.SetAdapterMode(CommonIdDef.CATEGORY_SUBTYPE_MODE_NORMAL);
        mListAdapter.notifyDataSetChanged();

        //mmSubTypesList.setSelection(0);
    }

    public boolean BackToUpLevel()
    {
        if(mCategorySubtypeMode != CommonIdDef.CATEGORY_SUBTYPE_MODE_NORMAL)
        {
            StartTypeNormal();
            //bug109817
            RefreshItemCount();
            return true;
        }
        return false;
    }
    OnQueryTextListener mOnQueryTextListener = new OnQueryTextListener()
    {

        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub
            mFileListManager.SearchString(mTypes,newText);
            mListAdapter.notifyDataSetChanged();
            RefreshItemCount();
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub
            mFileListManager.SearchString(mTypes,query);
            mListAdapter.notifyDataSetChanged();
            RefreshItemCount();
            return false;
        }

    };
    OnFooterItemClick mOnFooterItemClick = new OnFooterItemClick() {
        @Override
        public void onFooterItemClick(View view, int id) {
            Log.v(TAG,"Click FooterBarMenu Item. Id = " + id);
            switch(id)
            {
                case CommonIdDef.FOOTBAR_ITEM_ID_SORT:
                    CommonFunc.TA_Click(CommonFunc.OnClick_Sort);
                    StartSortDialog();
                    break;
                case CommonIdDef.FOOTBAR_ITEM_ID_COPY:
                    CommonFunc.TA_Click(CommonFunc.OnClick_Copy);
                    StartCopy();
                    break;
                case CommonIdDef.FOOTBAR_ITEM_ID_DELETE:
                    CommonFunc.TA_Click(CommonFunc.OnClick_Delete);
                    StartDelete();
                    break;
                case CommonIdDef.FOOTBAR_ITEM_ID_CUT:
                    CommonFunc.TA_Click(CommonFunc.OnClick_Cut);
                    StartCut();
                    break;
                case CommonIdDef.FOOTBAR_ITEM_ID_SHARE:
                    CommonFunc.TA_Click(CommonFunc.OnClick_Share);
                    StartShare();
                    break;
                case CommonIdDef.FOOTBAR_ITEM_ID_NEW_DIR:
                    CommonFunc.TA_Click(CommonFunc.OnClick_New);
                    break;
                case CommonIdDef.FOOTBAR_ITEM_ID_SHOW_HIDE_FILE:
                    CommonFunc.TA_Click(CommonFunc.OnClick_Hide);
                    boolean isHide = true;
                    Preferences.getSharedPreferences();
                    FileManagerSettings showHiddenPref = FileManagerSettings.SETTINGS_SHOW_HIDDEN;
                    if(Preferences.getSharedPreferences().getBoolean(showHiddenPref.getId(),
                             ((Boolean) showHiddenPref.getDefaultValue()).booleanValue()))
                    {
                        isHide = false;
                    }
                    try {
                        Preferences.savePreference(
                                FileManagerSettings.SETTINGS_SHOW_HIDDEN,
                                isHide, false);
                    } catch (InvalidClassException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    StartTypeNormal();
                    break;
                default:
                    break;
            }
        }
        };

    int mwhichButton;
    void StartSortDialog()
    {
        FileOrder mOrder = CommonFunc.loadCateorySaved(mActivity);

        AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(mActivity);

        mDialogBuilder.setSingleChoiceItems(R.array.filemanager_sort_mode, CommonFunc.fileOrder2Index(mOrder),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                try {
                                    mwhichButton = whichButton;
                                    // Enumeration
                                    CommonFunc.saveCateoryOrder(mActivity,CommonFunc.index2FileOrder(mwhichButton));
                                    mFileListManager.sortAllType();
                                    mIconDrawableManager.ChangeWorkingList(mFileListManager.GetWorkListFileString(),
                                            mmSubTypesList.getFirstVisiblePosition(),
                                            mmSubTypesList.getLastVisiblePosition());
                                    mListAdapter.notifyDataSetChanged();
                                    RefreshItemCount();
                                    dialog.dismiss();
                                    switch(whichButton)
                                    {
                                        case CommonFunc.NAME_INDEX:
                                            CommonFunc.TA_Click(CommonFunc.OnClick_Sort_name);
                                            break;
                                        case CommonFunc.SIZE_INDEX:
                                            CommonFunc.TA_Click(CommonFunc.OnClick_Sort_size);
                                            break;
                                        case CommonFunc.DATE_INDEX:
                                            CommonFunc.TA_Click(CommonFunc.OnClick_Sort_date);
                                            break;
                                        case CommonFunc.TYPE_INDEX:
                                            CommonFunc.TA_Click(CommonFunc.OnClick_Sort_type);
                                            break;
                                    }
                                } catch (Exception e) {
                                    Log.e(TAG, "Error applying navigation option", e); //$NON-NLS-1$
                                } finally {

                                }
                            }
                        });
                    mDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        /* User clicked Yes so do some stuff */
                    }
                });
        Dialog mDialog = mDialogBuilder.create();
        mDialog.show();
    }
    void StartCopy()
    {
        mCategorySubtypeMode = CommonIdDef.CATEGORY_SUBTYPE_MODE_COPY;
        StartSelectView(CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_COPY,R.string.actions_menu_Copy,mFuncCallClick);
    }
    void StartDelete()
    {
        mCategorySubtypeMode = CommonIdDef.CATEGORY_SUBTYPE_MODE_DELETE;
        StartSelectView(CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_DELETE,R.string.actions_menu_Delete,mFuncCallClick);

    }
    void StartCut()
    {
        mCategorySubtypeMode = CommonIdDef.CATEGORY_SUBTYPE_MODE_CUT;
        StartSelectView(CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_CUT,R.string.actions_menu_Cut,mFuncCallClick);

    }
    void StartShare()
    {
        mCategorySubtypeMode = CommonIdDef.CATEGORY_SUBTYPE_MODE_SHARE;
        StartSelectView(CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_SHARE,R.string.actions_menu_Share,mFuncCallClick);

    }
    private CheckBox cBox;
    void StartSelectView(int funcId,int FuncString,OnFooterItemClick m)
    {
        ((FileManagerAppFrame)mActivity).SetCanViewPagerMove(false);
        ActionBar bar = mActivity.getActionBar();
        ActionBarView mView = new ActionBarView(mActivity);//(ActionBarView)bar.getCustomView();
        /*mView.showBackKey(true,new OnBackKeyItemClick(){
            @Override
            public void onBackKeyItemClick() {
                BackToUpLevel();
            }});*/
        mView.setTitle(mActivity.getResources().getQuantityString(
                R.plurals.numberOfSelected,
                1 ,
                0
                ));
        //CheckBox cb
        cBox = new CheckBox(mActivity);
        //cb.setOnClickListener(mActionBarSelectCheckcb);
        mView.addRightItem(cBox);
        mView.setOnRightWidgetItemClickListener2(new OnRightWidgetItemClick2()
        {

            @Override
            public void onRightWidgetItemClick(View arg0) {
                // TODO Auto-generated method stub
             // TODO Auto-generated method stub
                CheckBox box = (CheckBox)arg0;
                if(!box.isChecked())
                {
                    box.setChecked(true);
                    mFileListManager.SelectedAll();
                }
                else
                {
                    box.setChecked(false);
                    mFileListManager.UnSelectedAll();
                }
                if(mListAdapter != null)
                    mListAdapter.notifyDataSetChanged();
                RefreshSelected();
            }

        });
        //mSearchView.setOnQueryTextListener(mOnQueryTextListener);
        ClearFootMenu();
        AddToFootButton(funcId,FuncString,m);
        mFileListManager.startSelect(mTypes,this);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(false);
        bar.setDisplayShowCustomEnabled(true);
        bar.setCustomView(mView);
        mListAdapter.SetAdapterMode(CommonIdDef.CATEGORY_SUBTYPE_MODE_SELECTING);
        mListAdapter.notifyDataSetChanged();
        RefreshItemCount();
    }
    void SelectItemClick(View view, int id)
    {
        FileInfo info = mFileListManager.getCurrentFileInfo(id);
        mFileListManager.getCurrentFileInfo(id).mSelected = !info.mSelected;

        ((DrawableText2ItemView)view).setEnabled(info.mSelected);
        mListAdapter.notifyDataSetChanged();
        RefreshItemCount();
        int lcount = mFileListManager.GetSelectedCount();
        if (info.mSelected) {
            mFileListManager.SelectedAdd();
            if (mFileListManager.GetSelectedCount() == mFileListManager.GetWorkingFileCount()) {
                setAllSelectBox(true);
                }
            }
        else
        {
            mFileListManager.SelectedDec();
                setAllSelectBox(false);
        }

        if(!((lcount > 0)&&(mFileListManager.GetSelectedCount()>0)))
        {
            if(mFileListManager.GetSelectedCount() > 0)
            {
                if (getFootButtonId() == CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_DELETE) {
                    mFooterBarButton.setItemTextColor(getFootButtonId(),
                             Color.RED);
             }
                else
                    mFooterBarButton.setItemTextColor(getFootButtonId(), mActivity.getResources().getColor(R.color.button_text_color_ok));
                mFooterBarButton.setItemEnable(getFootButtonId(), true);
                //mFooterBarButton.setClickable(true);
            }
            else
            {
                mFooterBarButton.setItemTextColor(getFootButtonId(), Color.GRAY);
                mFooterBarButton.setItemEnable(getFootButtonId(), false);
                //mFooterBarButton.setClickable(false);
            }
            mFooterBarButton.updateItems();
        }
        ActionBarView mbarview = (ActionBarView)(mActivity.getActionBar().getCustomView());
        mbarview.setTitle(mActivity.getResources().getQuantityString(
                R.plurals.numberOfSelected,
                mFileListManager.GetSelectedCount() ,
                mFileListManager.GetSelectedCount()
                ));
    }
    void RefreshSelected()
    {

        if(mFileListManager.GetSelectedCount() > 0)
        {
            if(getFootButtonId() == CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_DELETE)
                mFooterBarButton.setItemTextColor(getFootButtonId(), Color.RED);
            else
                mFooterBarButton.setItemTextColor(getFootButtonId(), mActivity.getResources().getColor(R.color.button_text_color_ok));
            mFooterBarButton.setItemEnable(getFootButtonId(), true);
            //mFooterBarButton.setClickable(true);
        }
        else
        {
            mFooterBarButton.setItemTextColor(getFootButtonId(), Color.GRAY);
            mFooterBarButton.setItemEnable(getFootButtonId(), false);
            //mFooterBarButton.setClickable(false);
        }
        mFooterBarButton.updateItems();

        ActionBarView mbarview = (ActionBarView)(mActivity.getActionBar().getCustomView());
        mbarview.setTitle(mActivity.getResources().getQuantityString(
                R.plurals.numberOfSelected,
                mFileListManager.GetSelectedCount() ,
                mFileListManager.GetSelectedCount()
                ));
    }
    int getFootButtonId()
    {
        int id = 0;
        switch(mCategorySubtypeMode)
        {
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_COPY:
                id = CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_COPY;
                break;
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_DELETE:
                id = CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_DELETE;
                break;
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_CUT:
                id = CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_CUT;
                break;
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_SHARE:
                id = CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_SHARE;
                break;

        }
        return id;
    }
    void StartItemLongPressClick(int id)
    {
        final FileInfo PressFileInfo = mFileListManager.getCurrentFileInfo(id);
        final FileInfo PressFileInfoList[] = {mFileListManager.getCurrentFileInfo(id)};
        final String PressString[] ={mFileListManager.getCurrentFileInfo(id).GetFullPath()};
        AlertDialog.Builder builder =  new AlertDialog.Builder(mActivity);
        builder.setItems(R.array.Category_longpress_item, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    /* User clicked so do some stuff */
                    String[] items = mActivity.getResources().getStringArray(R.array.Category_longpress_item);
                    if(items[which].equalsIgnoreCase(mActivity.getResources().getString(R.string.actions_menu_rename)))
                    {
                        FileRenameDialog mFileRenameDialog = new FileRenameDialog(mActivity,
                                R.string.actions_menu_rename,
                                PressFileInfo);
                        mFileRenameDialog.show(mThis);
                        CommonFunc.TA_Click(CommonFunc.LongPress_Rename);
                        //mFileRenameDialog.show(null);
                    }
                    else if(items[which].equalsIgnoreCase(mActivity.getResources().getString(R.string.actions_menu_Detail)))
                    {
                        FileDetailDialog mFileDetailDialog = new FileDetailDialog(mActivity,
                                R.string.actions_menu_Detail,
                                PressFileInfo);
                        mFileDetailDialog.show(null);
                        CommonFunc.TA_Click(CommonFunc.LongPress_Detail);
                    }
                    else if(items[which].equalsIgnoreCase(mActivity.getResources().getString(R.string.actions_menu_Crush)))
                    {
                        FileCrushDialog mFileCrushDialog = new FileCrushDialog(mActivity,PressFileInfoList);
                        mFileCrushDialog.show(mThis);
                        CommonFunc.TA_Click(CommonFunc.LongPress_Crush);
                    }
                    else if(items[which].equalsIgnoreCase(mActivity.getResources().getString(R.string.actions_menu_Delete)))
                    {
                        FileDeleteDialog mFileDeleteDialog = new FileDeleteDialog(mActivity,PressFileInfoList);
                        mFileDeleteDialog.show(mThis);
                        CommonFunc.TA_Click(CommonFunc.LongPress_Delete);
                    }
                    else if(items[which].equalsIgnoreCase(mActivity.getResources().getString(R.string.actions_menu_Share)))
                    {
                        CommonFunc.shareDialog(mActivity,PressFileInfoList);
                        CommonFunc.TA_Click(CommonFunc.LongPress_Share);
                    }
                    else if(items[which].equalsIgnoreCase(mActivity.getResources().getString(R.string.actions_menu_Copy)))
                    {
                        BackToUpLevel();
                        ((FileManagerAppFrame)mActivity).listLocalPathPaste(null, Mode.paste, SelectMode.copy,PressString);
                        CommonFunc.TA_Click(CommonFunc.LongPress_Copy);
                    }
                    else if(items[which].equalsIgnoreCase(mActivity.getResources().getString(R.string.actions_menu_Cut)))
                    {
                        BackToUpLevel();
                        ((FileManagerAppFrame)mActivity).listLocalPathPaste(null, Mode.paste, SelectMode.cut,PressString);
                        CommonFunc.TA_Click(CommonFunc.LongPress_Cut);
                    }
                    else
                    {
/*                    new AlertDialog.Builder(mActivity)
                            .setMessage("You selected: " + which + " , " + items[which])
                            .show();*/
                    }
                }
            });
            //View v3 = DialogUtils.buildCustomView(mActivity, R.string.select_dialog);
            //builder.setCustomTitle(v3);

            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Cancel so do some stuff */
                }
            });

        Dialog dd = builder.create();
        dd.show();
        //DialogUtils.fromBottomToTop(dd);

    }

    OnFooterItemClick mFuncCallClick = new OnFooterItemClick() {
        @Override
        public void onFooterItemClick(View view, int id) {
            final FileInfo[] PressFileInfo;
            final String[] PressString;
            switch (id) {
                case CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_CANCEL:
                    BackToUpLevel();
                    break;
                case CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_COPY:
                    PressString = mFileListManager.GetSelectCurrentFileString();
                    if(PressString.length < 1)
                        return;
                    BackToUpLevel();
                    ((FileManagerAppFrame)mActivity).listLocalPathPaste(null, Mode.paste, SelectMode.copy,PressString);
                    break;
                case CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_DELETE:
                    PressFileInfo = mFileListManager.GetSelectCurrentFileInfo();
                    if(PressFileInfo.length < 1)
                        return;
                    BackToUpLevel();
                    FileDeleteDialog mFileDeleteDialog = new FileDeleteDialog(mActivity,PressFileInfo);
                    mFileDeleteDialog.show(mThis);
                    break;
                case CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_SHARE:
                    PressFileInfo = mFileListManager.GetSelectCurrentFileInfo();
                    if(PressFileInfo.length < 1)
                        return;
                    BackToUpLevel();
                    CommonFunc.shareDialog(mActivity,PressFileInfo);
                    break;
                case CommonIdDef.FOOTBAR_BUTTON_ID_SHOW_CUT:
                    PressString = mFileListManager.GetSelectCurrentFileString();
                    if(PressString.length < 1)
                        return;
                    BackToUpLevel();
                    ((FileManagerAppFrame)mActivity).listLocalPathPaste(null, Mode.paste, SelectMode.cut,PressString);
                    break;

            }
        }
    };

    OnClickListener mActionBarSelectCheckcb = new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            CheckBox box = (CheckBox)v;
            if(box.isChecked())
            {
                box.setChecked(true);
                mFileListManager.SelectedAll();
            }
            else
            {
                box.setChecked(false);
                mFileListManager.UnSelectedAll();
            }
            if(mListAdapter != null)
                mListAdapter.notifyDataSetChanged();
            RefreshSelected();
            CommonFunc.TA_Click(CommonFunc.OnClick_Delete_All);
        }

    };
    void initCrumbs()
    {
        mTitle = (LinearLayout)mSubTypesView.findViewById(R.id.dir_container);
        mTitle.removeAllViews();

        LinearLayout ItemMain = (LinearLayout)LayoutInflater.from(mActivity).inflate(R.layout.crumbs_item, null);
        ItemMain.setBackgroundResource(R.drawable.aui_crumb_bg_main);
        ItemMain.setClickable(true);
        TextView titleMain= (TextView)ItemMain.findViewById(R.id.dir_name);
        titleMain.setText(mActivity.getString(R.string.CategoryBrowse));
        mTitle.addView(ItemMain);

        ItemMain.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(mCategorySubtypeMode == CommonIdDef.CATEGORY_SUBTYPE_MODE_NORMAL)
                {
                    mHandler.sendEmptyMessage(CommonIdDef.CATEGORY_MESSAGE_ID_BACK);

                }
                //bug109817
                else if(mCategorySubtypeMode == CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCHING)
                {
                    mSearchView.clearFocus();
                    StartTypeNormal();
                    mHandler.sendEmptyMessage(CommonIdDef.CATEGORY_MESSAGE_ID_BACK);
                }
            }
        });


        LinearLayout ItemSub = (LinearLayout)LayoutInflater.from(mActivity).inflate(R.layout.crumbs_item, null);
        ItemSub.setBackgroundResource(R.drawable.aui_crumb_bg_sub);
        ItemSub.setClickable(true);
        TextView titlesub= (TextView)ItemSub.findViewById(R.id.dir_name);
        titlesub.setText(mActivity.getString(R.string.CategoryBrowse));
        //TextView titleSub= new TextView(mActivity);
        String title = new String();
        switch(mTypes)
        {
            case CommonIdDef.CATEGORY_ITEM_ID_PICTURE:
                title = mActivity.getString(R.string.CategoryItemPic);
                break;
            case CommonIdDef.CATEGORY_ITEM_ID_MUSIC:
                title =mActivity.getString(R.string.CategoryItemMusic);
                break;
            case CommonIdDef.CATEGORY_ITEM_ID_VIDEO:
                title =mActivity.getString(R.string.CategoryItemVideo);
                break;
            case CommonIdDef.CATEGORY_ITEM_ID_PACKAGE:
                title =mActivity.getString(R.string.CategoryItemApk);
                break;
            case CommonIdDef.CATEGORY_ITEM_ID_TEXT:
                title =mActivity.getString(R.string.CategoryItemText);
                break;
            case CommonIdDef.CATEGORY_ITEM_ID_ZIP:
                title =mActivity.getString(R.string.CategoryItemZip);
                break;
            default:
                break;
        }
        titlesub.setText(title);
        LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        margin.setMargins((int)(-1*AndroidHelper.convertDpToPixel(mActivity, 10)), 0, 0, 0);
        mTitle.addView(ItemSub,margin);

    }

    @Override
    public void FileListChanged(int curType) {
        // TODO Auto-generated method stub
        //mListAdapter.notifyDataSetChanged();
        mHandler.post(new Runnable()
        {
            @Override
            public void run() {
                // TODO Auto-generated method stub


                mFileListManager.ForceRefreshWorkingList();
                mIconDrawableManager.ChangeWorkingList(mFileListManager.GetWorkListFileString(),
                        mmSubTypesList.getFirstVisiblePosition(),
                        mmSubTypesList.getLastVisiblePosition());

                if(mListAdapter != null)
                    mListAdapter.notifyDataSetChanged();
                RefreshItemCount();
                RefreshFootMenu();
            }

        });
    }
    @Override
    public int Resultcb(int Res, String r) {
                // TODO Auto-generated method stub
                Log.v(TAG, "Resultcb Res:" + Res);
                Log.v(TAG, "Resultcb r:" + r);
                String path = r.substring(0, r.lastIndexOf(File.separator));
                Log.v(TAG, "Resultcb path:" + path);
                if(Res == 0)
                {
                    mFileListManager.syncDir(path, false,null);
                }
                return 0;
    }
    void initIconMgr()
    {

        mIconDrawableManager.ChangeWorkingList(mFileListManager.GetWorkListFileString(),
                0,
                9);
    }

    @Override
    public Drawable GeticonByObject(String input) {
        // TODO Auto-generated method stub
//        Log.v(TAG, "GeticonByObject path:" + input);
        File file = new File(input);
        if(!file.exists())
            return null;
        Drawable ret = null;
        String thumbnailPath = null;
        String ext = input.substring(input.lastIndexOf("."));
//        Log.v(TAG, "GeticonByObject ext:" + ext);
        if(ext.equalsIgnoreCase(".jpg")||ext.equalsIgnoreCase(".png"))
        {
            thumbnailPath = IconDrawableManager.GetThumbnailPath(mActivity,input);
//            Log.v(TAG, "GeticonByObject thumbnailPath:" + thumbnailPath);
            Bitmap temp = null;
            if(thumbnailPath == null)
            {
                temp = ThumbnailUtils.createImageThumbnail(input, Images.Thumbnails.MINI_KIND);
            }
            else
            {
                temp = BitmapFactory.decodeFile(thumbnailPath);
            }
            //bug113329, modify by wushuan
            if(temp == null)
                temp = ThumbnailUtils.createImageThumbnail(input, Images.Thumbnails.MINI_KIND);
            Bitmap temp1 = CommonFunc.ResizeBitmap2fitDrawable(mActivity,temp);
            if(temp1 != null)
                ret = new BitmapDrawable(mActivity.getResources(),temp1);
        }
        else if(ext.equalsIgnoreCase(".3gp")||ext.equalsIgnoreCase(".mp4"))
        {
            thumbnailPath = IconDrawableManager.GetThumbnailPath(mActivity,input);
//            Log.v(TAG, "GeticonByObject thumbnailPath:" + thumbnailPath);
            Bitmap temp = null;
            if(thumbnailPath == null)
            {
                temp = ThumbnailUtils.createVideoThumbnail(input, Images.Thumbnails.MINI_KIND);
            }
            else
            {
                temp = BitmapFactory.decodeFile(thumbnailPath);
            }
            if(temp == null)
                temp = ThumbnailUtils.createVideoThumbnail(input, Images.Thumbnails.MINI_KIND);
            Bitmap temp1 = CommonFunc.ResizeBitmap2fitDrawable(mActivity,temp);
            if(temp1 != null)
                ret = new BitmapDrawable(mActivity.getResources(),temp1);
        }
        else if(ext.equalsIgnoreCase(".apk"))
        {
            ret = CommonFunc.showUninstallAPKIcon(input, mActivity);
        }

        return ret;
    }

    @Override
    public int IconCacheChangeNotify(int status) {
        // TODO Auto-generated method stub
        Log.v(TAG, "IconCacheChangeNotify status:" + status);
        //mListAdapter.notifyDataSetChanged();
        mHandler.post(new Runnable()
        {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                if(mListAdapter != null)
                    mListAdapter.notifyDataSetChanged();
                RefreshItemCount();
            }

        });
        return 0;
    }
    public void RefreshIconDrawableManager()
    {

    }
    public void RefreshItemCount()
    {
        if(mListAdapter == null)
            return ;

        if(mCategorySubtypeMode == CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCHING)
        {
            mNoItemImageView.setCompoundDrawables(null, null, null, null);
            mNoItemImageView.setText(R.string.No_item_Searched);
        }
        else
        {
            Drawable drawable= mActivity.getResources().getDrawable(R.drawable.ic_file_empty_normal);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            mNoItemImageView.setCompoundDrawables(null, drawable, null, null);
            mNoItemImageView.setCompoundDrawablePadding((int)AndroidHelper.convertDpToPixel(mActivity, 24));
            mNoItemImageView.setGravity(Gravity.CENTER);
            mNoItemImageView.setText(R.string.No_item_display);
        }
        if(mListAdapter.getCount() == 0)
        {
            mNoItemImageView.setVisibility(View.VISIBLE);
            mmSubTypesList.setVisibility(View.INVISIBLE);
        }
        else
        {
            mNoItemImageView.setVisibility(View.INVISIBLE);
            mmSubTypesList.setVisibility(View.VISIBLE);
        }
    }
    @Override
    public int FileDeleteResultcb(int Res, String r) {
        // TODO Auto-generated method stub
        Log.v(TAG, "FileDeleteResultcb path:" + r);
        Log.v(TAG, "FileDeleteResultcb count:" + Res);
        if(Res > 0)
        {
            mFileListManager.syncDir(r, true,null);
            MediaScannerConnection.scanFile(mActivity, new String[]{ r }, null,null);
        }
        return 0;
    }
    @Override
    public int FileCrushResultcb(int Res, String r) {
        // TODO Auto-generated method stub
     // TODO Auto-generated method stub
        Log.v(TAG, "FileCrushResultcb path:" + r);
        Log.v(TAG, "FileCrushResultcb count:" + Res);
        if(Res > 0)
        {
            mFileListManager.syncDir(r, true,null);
        }
        return 0;
    }

    private void setAllSelectBox(boolean isAllSelect) {
        if (cBox != null) {
            cBox.setChecked(isAllSelect);
            }
    }
}
